# In this file we define which of our own applications/tools/test should be built. 
# For docs see: http://software.intel.com/sites/landingpage/pintool/docs/62141/Pin/html/index.html#AddingTests
# For a complex example see: pin/source/tools/SimpleExamples/makefile.rules

# DataTracker top dir
DTRACKER_ROOT		= $(abspath $(dir $(firstword $(MAKEFILE_LIST))))

# Paths for support libraries.
SUPPORT_REL_PATH	= ./support
SUPPORT_PATH		= $(realpath $(SUPPORT_REL_PATH))
LIBDFT_PATH			= $(realpath $(SUPPORT_PATH)/libdft/src)
#LIBDFT_TAG_FLAGS	?= -DLIBDFT_TAG_TYPE=libdft_tag_set_fdoff
#LIBDFT_TAG_FLAGS	?= -DLIBDFT_TAG_TYPE=libdft_tag_bitset -DTAG_BITSET_SIZE=32
#LIBDFT_TAG_FLAGS	?= -DLIBDFT_TAG_TYPE=libdft_tag_bvector
LIBDFT_TAG_FLAGS	?= -DLIBDFT_TAG_TYPE=libdft_tag_ewah

# Turns pintool.log output off.
LOGGING_FLAGS = -DNO_PINTOOL_LOG

# Pin Makefile variables.
TOOL_ROOTS := dtracker
APP_ROOTS := 
TEST_ROOTS := 
TOOL_CXXFLAGS += -std=c++11 $(LOGGING_FLAGS) $(LIBDFT_TAG_FLAGS) -I$(DTRACKER_ROOT) -I$(LIBDFT_PATH)

#######################################################################
# Data Tracker.
#######################################################################
DTRACKER_HOOKS_ACTIVE	= $(shell echo "LIBDFT_TAG_TYPE" | $(CPP) $(LIBDFT_TAG_FLAGS) - | tail -1)
DTRACKER_HOOKS_DIR		= hooks/$(DTRACKER_HOOKS_ACTIVE)
DTRACKER_HOOKS_SRC		= $(wildcard $(DTRACKER_HOOKS_DIR)/*.cpp)
DTRACKER_HOOKS_OBJS		= $(patsubst %.cpp,$(OBJDIR)%$(OBJ_SUFFIX),$(DTRACKER_HOOKS_SRC))

DTRACKER_OBJS			= $(OBJDIR)dtracker$(OBJ_SUFFIX)\
						  $(DTRACKER_HOOKS_OBJS)\
						  $(OBJDIR)provlog$(OBJ_SUFFIX)\
						  $(OBJDIR)osutils$(OBJ_SUFFIX)\
						  $(OBJDIR)dtracker_debug$(OBJ_SUFFIX)

$(OBJDIR)dtracker$(OBJ_SUFFIX): dtracker.cpp | $(OBJDIR)hooks/$(DTRACKER_HOOKS_ACTIVE)
	$(CXX) $(TOOL_CXXFLAGS) $(COMP_OBJ)$@ $<

# Note: $(TOOL_LIBS) must come after -ldft, or you'll get undefined symbols at runtime.
$(OBJDIR)dtracker$(PINTOOL_SUFFIX): $(DTRACKER_OBJS)
	$(LINKER) $(TOOL_LDFLAGS) $(LINK_EXE)$@ $(^:%.h=) $(TOOL_LPATHS) -L$(LIBDFT_PATH) -ldft $(TOOL_LIBS)

#######################################################################
# Directories.
#######################################################################
$(OBJDIR)hooks/$(DTRACKER_HOOKS_ACTIVE):
	mkdir -p $@

#######################################################################
# Generic rules for support libraries.
#######################################################################
SUPPORT_VARS += CC="$(CC)" CFLAGS="$(TOOL_CFLAGS)"
SUPPORT_VARS += CXX="$(CXX)" CXXFLAGS="$(TOOL_CXXFLAGS)"

.PHONY: support-%
support-%:
	export LIBDFT_TAG_FLAGS
	$(MAKE) -C support -f makefile.$*

.PHONY: help
help:
	$(info Some potentially useful targets:)
	$(info - support : Builds libraries to be used for writing/compiling pin tools.)
	$(info - support-clean : Remove the built libraries.)
	$(info 		)
	$(info Some potentially useful options:)
	$(info - DEBUG=1 : Turns off optimizations and enables debug flags.)
	$(info - USE_GLIB=1 : Enable compile/linking flags for glib.)

# vim: set noet ts=4 sts=4 sw=4 ai ft=make :
